<template>
  <div class="execution">
      <el-form inline :model="dataForm" v-if="searchFormShow">
        <el-row class="bg-row">
          <el-col :span="6">
            <el-form-item label="学校名称">
              <el-input v-model="dataForm.schoolName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="部门名称">
              <el-input v-model="dataForm.deptName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="部门编号">
              <el-input v-model="dataForm.deptCode"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="部门职责">
              <el-input v-model="dataForm.responsibility"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button size="mini" icon="el-icon-search" type="primary" @click="searchHandle">查询</el-button>
              <el-button size="mini" icon="el-icon-delete" type="info" @click="clearSearchForm">清空</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div style="margin-top: 5px;margin-bottom: 5px;">
        <el-row style="margin-top: 5px;">
          <el-button v-if="permissions.eims_allocation_add" icon="el-icon-plus" type="primary" @click="addOrUpdateHandle('','add')">新增</el-button>
          <el-tooltip class="item" effect="dark" content="搜索" placement="top"style="float: right;">
            <el-button icon="el-icon-search" circle @click="searchFormShow = !searchFormShow"></el-button>
          </el-tooltip>
        </el-row>
      </div>

      <el-table :data="dataList" height="calc(100vh - 215px)" border stripe v-loading="dataListLoading">
          <el-table-column prop="deptName" label="部门名称" min-width="120"></el-table-column>
          <el-table-column prop="deptCode" label="部门编号" min-width="100"></el-table-column>
          <el-table-column prop="schoolName" label="学校名称" min-width="180"></el-table-column>
          <el-table-column prop="responsibility" label="部门职责" min-width="180"></el-table-column>
          <el-table-column label="操作" width="230" fixed="right">
            <template slot-scope="scope">
              <el-button type="text" icon="el-icon-view" @click="addOrUpdateHandle(scope.row.id,'check')">详情</el-button>
              <el-button type="text" icon="el-icon-edit" @click="addOrUpdateHandle(scope.row.id,'edit')">编辑</el-button>
              <el-button type="text" icon="el-icon-delete" @click="deleteHandle(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      <div class="avue-crud__pagination" style="margin-bottom: 25px;">
        <el-pagination
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle"
          :current-page="pageIndex"
          :page-sizes="[10, 20, 50, 100, 200]"
          :page-size="pageSize"
          :total="totalPage"
          background
          layout="total, sizes, prev, pager, next">
        </el-pagination>
      </div>
      <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></table-form>
  </div>
</template>

<script>
  import { filterParams } from '@/util/tools'
  import { fetchList, delObj } from '@/api/pvm/dept'
  import { mapGetters } from 'vuex'

  import TableForm from './TableForm'

  export default {
    name: 'pvmdept',
    data() {
      return {
        searchFormShow: true,
        dataList: [],
        dataListLoading: false,
        pageIndex: 1,
        pageSize: 100,
        totalPage: 0,
        dataForm: {},
        addOrUpdateVisible: false,
      }
    },
    components: {
      TableForm
    },
    computed: {
      ...mapGetters(['permissions']),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permissions.admin_pvmschool_add, false),
          delBtn: this.vaildData(this.permissions.admin_pvmschool_del, false),
          editBtn: this.vaildData(this.permissions.admin_pvmschool_edit, false)
        };
      }
    },
    created() {
      this.searchHandle()
      this.admin_pvmschool_add = this.permissions['admin_pvmschool_add']
      this.admin_pvmschool_edit = this.permissions['admin_pvmschool_edit']
      this.admin_pvmschool_del = this.permissions['admin_pvmschool_del']
    },
    methods: {
      // 查询教师数据
      searchHandle(){
        this.pageIndex = 1
        this.getDataList()
      },
      // 清空查询表单
      clearSearchForm(){
        for(let key in this.dataForm){
          this.dataForm[key] = ''
        }
      },
      // 获取教师数据列表
      getDataList(){
        let params1 = {current: this.pageIndex,size: this.pageSize}
        let params2 = filterParams(this.dataForm)
        this.dataListLoading = true
        fetchList(Object.assign(params1,params2)).then(response => {
          if(response.data.code == '0'){
            this.dataList = response.data.data.records
            this.totalPage = response.data.data.total
          }else{
            this.$message.error(response.data.mdg)
          }
          this.dataListLoading = false
        })
      },
      // 每页条数改变
      sizeChangeHandle(val){
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页改变
      currentChangeHandle(val){
        this.pageIndex = val
        this.getDataList()
      },
      // 打开弹窗 新增 / 修改 / 详情
      addOrUpdateHandle(id,type){
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id,type)
        })

      },
      // 删除教师
      deleteHandle(row){
        this.$confirm('此操作将永久删除该部门(部门名为:' + row.deptName + '), 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return delObj(row.id)
        }).then(response => {
          if(response.data.code == '0'){
            this.$message.success('删除成功')
            this.getDataList()
          }else{
            this.$message.error(response.data.mdg)
          }
        })
      },
      //  转换类型格式
      statusFormat(val){
        let obj = {}
        obj = this.statusOption.find(item => {
          if(item.value === val){
            return item
          }
        })
        if(obj && obj.label){
          return obj.label
        }
      },
      //获取省级地址列表
      getProvincesList() {
        var parentId = "86";
        getListByParentId(parentId).then(response => {
          if(response.data.code == '0'){
            this.provincesOptions = response.data.data
          }else{
            this.$message.error(response.data.mdg)
          }
        }).catch(() => {
          alert("省级地址出错了，请联系管理员")
        });
      },
      //根据省级地址编码获取市级地址列表
      setCityList: function(parentId) {
        this.cityCode = "";
        this.areaCode = "";
        if (this.provinceCode != "") {
          getListByParentId(parentId).then(response => {
            if(response.data.code == '0'){
              this.citysOptions = response.data.data
            }else{
              this.$message.error(response.data.mdg)
            }
          }).catch(() => {
            alert("市级地址出错了，请联系管理员")
          })
        }
      },
      //根据市级地址编码获取区级地址列表
      setAreaList: function(parentId) {
        this.areaCode = "";
        if (this.cityCode != "") {
          getListByParentId(parentId).then(response => {
            if(response.data.code == '0'){
              this.areasOptions = response.data.data
            }else{
              this.$message.error(response.data.mdg)
            }
          }).catch(() => {
            alert("市级地址出错了，请联系管理员")
          })
        }
      }
    }
  }
</script>
